﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Net.Mail;
using System.IO;
using System.Text.RegularExpressions;
using System;
using System.Security.Cryptography;
using System.Text;

public partial class Profile_ : System.Web.UI.Page
{
    string imaurl = "~//data/logos//default.jpg";
    int logodefault = 0;
    public static string tempo;
    string puser = "";
    rUser userinfo;
    DataSet dst = null, companies = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null) Response.Redirect("../Default.aspx");

        if (Page.IsPostBack)
        {
            tempo = NewPass.Text;
            if (userinfo == null)
            {
                userinfo = (rUser)Session["userProfile"];
            }
        }
        else
        {
            restricted();
            permiso();

            try
            {
                puser = Request.QueryString["id"].ToString();
                com.bpophotos.www.Service service = new com.bpophotos.www.Service();

                string info = service.getTodosDatosUsuario(puser);
                userinfo = new rUser();
                userinfo = Utility.getRUser(info);
            }
            catch(Exception ex){

                userinfo = new rUser();
                userinfo= (rUser)Session["userid"];
                puser = userinfo.getUsuarioId();
            }
            if (!puser.Equals("")){
          
            fillstates();
            pintarinfo(userinfo);
            NewPass.Text = tempo;
            repass.Text = tempo;
            }

            Session["userProfile"] = userinfo;
        }
        cbPState.Width = 202;
        cbBState.Width = 202;
    }

    private void pintarinfo(rUser userinfo)
    {
        //****** Pintando Profile *****************
        imaurl = userinfo.getLogo();
        Image5.ImageUrl = imaurl;
        firstname.Text = userinfo.getNombre();
        lastname.Text = userinfo.getApellido();
        initial.Text = userinfo.getAlias();
        email.Text = userinfo.getEmail();
        company.Text = userinfo.compname; 

        title.Text = userinfo.title;
        user.Text = userinfo.getUsuarioId();
        Image5.ImageUrl = userinfo.getLogo();
        OldPass.Text = userinfo.pass;
        if (userinfo.direcciones.Count != 0)
        {
            RE_Direccion rdir1 = (RE_Direccion)(userinfo.direcciones[0]);
            if (rdir1 != null)
            {
                paddress1.Text = rdir1.stnumber;
                paddress2.Text = rdir1.stadd;
                pcity.Text = rdir1.city;
                if (rdir1.state != null) { cbPState.SelectedValue = rdir1.state.Trim(); }
                pzip.Text = rdir1.zip;
                officephone.Text = rdir1.telof;
                cellphone.Text = rdir1.telcel;
                Fax.Text = rdir1.fax;
            }
            //******* Pintando Billing Info *************
            RE_Direccion rdir2 = (RE_Direccion)(userinfo.direcciones[1]);
            if (rdir2 != null)
            {
                baddress1.Text = rdir2.stnumber;
                baddress2.Text = rdir2.stadd;
                bcity.Text = rdir2.city;
                if (rdir2.state != null) { cbBState.SelectedValue = rdir2.state.Trim(); }
                bzip.Text = rdir2.zip;
            }
            //****** Pintado Credit Card Info ************
            RE_CreditCard rtarjetas = (RE_CreditCard)(rdir1.tarjetas[0]);
            if (rtarjetas != null)
            {
                namecreditcard.Text = rtarjetas.name;
                creditcard.Text = rtarjetas.number;
                ExpDate.Text = rtarjetas.expdate;
                cvv.Text = Convert.ToString(rtarjetas.ccv);
            }
        }
        if (imaurl.IndexOf("DEFAULT.") != -1)
        {
            CheckBox1.Checked = true;
        }
        else if (imaurl.IndexOf("DEFAULT1.") != -1)
        {
            CheckBox2.Checked = true;
        }
        else if (imaurl.IndexOf("LOGOCASA") != -1)
        {
            CheckBox3.Checked = true;
        }
    }
   
    //boton submit
   
    private string crearXml()
    {
        string statePinitial = cbPState.Text; //DB.getStateInitial(cbPState.Text);
        string stateBinitial = cbBState.Text; //DB.getStateInitial(cbBState.Text);

        string sxml = "";
        sxml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <datos_registro><user>";
        sxml = sxml +  user.Text;
        sxml = sxml + "</user><pass>" + NewPass.Text;
        sxml = sxml + "</pass><name>" + firstname.Text + "</name>";
        sxml = sxml + "<lastname>"+ lastname.Text +"</lastname>";
        sxml = sxml + "<alias>" + initial.Text.ToUpper() +"</alias>";
        sxml = sxml + "<email>" + email.Text + "</email>";
        sxml = sxml + "<comp-name>" + company.Text + "</comp-name>";
        sxml = sxml + "<title>" + title.Text + "</title>";
        sxml = sxml += "<existe></existe>";
        sxml = sxml + "<logo>" + imaurl + "</logo>";
        sxml = sxml + "<direcciones>";

        //para profile
        sxml = sxml + "<direccion><add1>" + paddress1.Text + "</add1>";
        sxml = sxml + "<add2>" + paddress2.Text + "</add2>";
        sxml = sxml + "<city>" + pcity.Text + "</city>";
        sxml = sxml + "<state>" + statePinitial + "</state>";
        sxml = sxml + "<zip>" + pzip.Text + "</zip>";
        sxml = sxml + "<office-phone>" + officephone.Text + "</office-phone>";
        sxml = sxml + "<cell-phone>" + cellphone.Text + "</cell-phone>";
        sxml = sxml + "<fax>" + Fax.Text + "</fax>";
        sxml = sxml + "<tipo>1</tipo>";
        sxml = sxml + "<uidID>" + DB.getuin_ID(user.Text.ToUpper(), 1) + "</uidID>";
        sxml = sxml + "<credit_card><tacID>1</tacID>";
        sxml = sxml + "<namecreditcard>"+namecreditcard.Text+"</namecreditcard>";
        sxml = sxml + "<creditcard>"+creditcard.Text+"</creditcard>";
        sxml = sxml + "<expdate>"+ExpDate.Text+"</expdate>";
        sxml = sxml + "<ccv>"+cvv.Text+"</ccv>";
        sxml = sxml + "</credit_card></direccion>";

        //Dir Billing

        sxml = sxml + "<direccion><add1>" + baddress1.Text + "</add1>";
        sxml = sxml + "<add2>" + baddress2.Text + "</add2>";
        sxml = sxml + "<city>" + bcity.Text + "</city>";
        sxml = sxml + "<state>" + stateBinitial + "</state>";
        sxml = sxml + "<zip>" + bzip.Text + "</zip>";
        sxml = sxml + "<office-phone></office-phone>";
        sxml = sxml + "<cell-phone></cell-phone>";
        sxml = sxml + "<fax></fax>";
        sxml = sxml + "<tipo>2</tipo>";
        sxml = sxml + "<uidID>" + DB.getuin_ID(user.Text.ToUpper(), 2) + "</uidID>";
        sxml = sxml + "<credit_card><tacID>1</tacID>";
        sxml = sxml + "<namecreditcard>" + namecreditcard.Text + "</namecreditcard>";
        sxml = sxml + "<creditcard>" + creditcard.Text + "</creditcard>";
        sxml = sxml + "<expdate>" + Fax.Text + "</expdate>";
        sxml = sxml + "<ccv>" + cvv.Text + "</ccv>";
        sxml = sxml + "</credit_card></direccion>";

        sxml = sxml + "</direcciones>";
        sxml = sxml + "</datos_registro>";
       
        return sxml;
    }

    private int validar()
    {
        int result = 1;
        //if (File1.PostedFile != null) { logodefault = 4; }
        if (email.Equals("")) { result = result * 0; }
        if (cbBState.SelectedIndex == 0) { result = result * 0; }
        if (cbPState.SelectedIndex == 0) { result = result * 0; }
        if (NewPass.Enabled.ToString().ToUpper() == "TRUE")
        {
            if ((OldPass.Text != "" || OldPass.Text== "" ) && ((NewPass.Text != repass.Text) || (NewPass.Text == "" && repass.Text == "")))
            {
                result = result * 0;
                OldPass.Text = string.Empty; NewPass.Text = string.Empty; repass.Text = string.Empty;
                OldPass.Enabled = false; NewPass.Enabled = false; repass.Enabled = false;
            }
            
        }   
            /*else
            {
                OldPass.Text = string.Empty; NewPass.Text = string.Empty; repass.Text = string.Empty;
                OldPass.Enabled = false; NewPass.Enabled = false; repass.Enabled = false;
            }*/
        //if (logodefault==0 || logodefault>4){result=result*0;}
        return result;
        
    }

    private void guardarlogo(rUser rreg)
    {
       
        if (File1.PostedFile != null)
        {
            try
            {
                
                
                string filepath = File1.PostedFile.FileName;
                                            
                File1.PostedFile.SaveAs(Server.MapPath(".//data/logos//U"+rreg.getUsuarioId()+"_"+filepath) );
               //update logo for user WS

            }
            catch (Exception ex)
            {
                Span3.InnerHtml = "Error saving file <b>"+File1.PostedFile.FileName;
                  
            }
        }
       
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1.Checked==true){
            CheckBox2.Checked = false;
            CheckBox3.Checked = false;
            logodefault = 1;
        }
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox2.Checked == true)
        {
            CheckBox1.Checked = false;
            CheckBox3.Checked = false;
            logodefault = 2;
        }
    }
    protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox3.Checked == true)
        {
            CheckBox1.Checked = false;
            CheckBox2.Checked = false;
            logodefault = 3;
        }
    }
    protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox4.Checked == true)
        {
            baddress1.Text = paddress1.Text;
            baddress2.Text = paddress2.Text;
            bcity.Text = pcity.Text;
            cbBState.SelectedValue = cbPState.SelectedValue;
            bzip.Text = pzip.Text;
        }
        else {
            baddress1.Text = "";
            baddress2.Text = "";
            bcity.Text = "";
            cbBState.SelectedIndex=0;
            bzip.Text = "";
        
        }
    }
    protected void CheckBox5_CheckedChanged(object sender, EventArgs e)
    {
        logodefault = 4;
    }

    protected void office1_TextChanged(object sender, EventArgs e)
    {

        if (officephone.Text.Length == 10)
        {
            string toffice;


            toffice = "(" + officephone.Text[0].ToString() + officephone.Text[1].ToString() + officephone.Text[2].ToString() + ")"
                + officephone.Text[3].ToString() + officephone.Text[4].ToString() + officephone.Text[5].ToString() + "-"
                + officephone.Text[6].ToString() + officephone.Text[7].ToString() + officephone.Text[8].ToString() + officephone.Text[9].ToString();
            officephone.Text = toffice;
            //RegularExpressionValidator6.Enabled = false;

        }
        else
        {
            if (officephone.Text.Length != 13)
            {
               // RegularExpressionValidator6.Enabled = true;

            }

        }






    }
    protected void cellphone_textchanged(object sender, EventArgs e)
    {
        if (cellphone.Text.Length == 10)
        {
            string toffice;



            toffice = "(" + cellphone.Text[0].ToString() + cellphone.Text[1].ToString() + cellphone.Text[2].ToString() + ")"
                + cellphone.Text[3].ToString() + cellphone.Text[4].ToString() + cellphone.Text[5].ToString() + "-"
                + cellphone.Text[6].ToString() + cellphone.Text[7].ToString() + cellphone.Text[8].ToString() + cellphone.Text[9].ToString();
            cellphone.Text = toffice;

            //RegularExpressionValidator7.Enabled = false;

        }
        else
        {
            if (cellphone.Text.Length != 13)
            {
                //RegularExpressionValidator7.Enabled = true;

            }
        }
    }
    protected void faxphone_textchanged(object sender, EventArgs e)
    {

        if (Fax.Text.Length == 10)
        {
            string toffice;
            toffice = "(" + Fax.Text[0].ToString() + Fax.Text[1].ToString() + Fax.Text[2].ToString() + ")"
                + Fax.Text[3].ToString() + Fax.Text[4].ToString() + Fax.Text[5].ToString() + "-"
                + Fax.Text[6].ToString() + Fax.Text[7].ToString() + Fax.Text[8].ToString() + Fax.Text[9].ToString();
            Fax.Text = toffice;
            //RegularExpressionValidator8.Enabled = false;
        }
        else
        {
            if (Fax.Text.Length != 13)
            {
                //RegularExpressionValidator8.Enabled = true;
            }
        }
    }

    protected void Button1_Click(object sender, System.EventArgs e)
    {
        lboldpass.CssClass = "style14";
        OldPass.CssClass = "txtForm";
        OldPass.Enabled = true;

        lbnewpass.CssClass = "style14";
        NewPass.CssClass = "txtForm";
        NewPass.Enabled = true;

        lblrepass.CssClass = "style14";
        repass.CssClass = "txtForm";
        repass.Enabled = true;
        


    }
    
    protected void btnchangePict(object sender, System.EventArgs e)
    {
               

    }

         

    protected void compname_TextChanged(object sender, System.EventArgs e)
    {

    }
 
    protected void firstname_TextChanged(object sender, System.EventArgs e)
    {

    }
    protected void bcity_TextChanged(object sender, System.EventArgs e)
    {

    }

    private void fillstates()
    {
        dst = DB.GetStates();
        cbPState.Items.Add("Select One");
        cbBState.Items.Add("Select One");
        for (int i = 0; i <= (dst.Tables[0].Rows.Count - 1); i++)
        {
            if (dst.Tables[0].Rows[i]["enable"].ToString().Equals("1"))
            {
                ListItem st1 = new ListItem();
                ListItem st2 = new ListItem();
                st1.Text = Utility.toCapital(dst.Tables[0].Rows[i]["name"].ToString());
                st1.Value = dst.Tables[0].Rows[i]["initial"].ToString();
                cbPState.Items.Add(st1);
                st2.Text = Utility.toCapital(dst.Tables[0].Rows[i]["name"].ToString());
                st2.Value = dst.Tables[0].Rows[i]["initial"].ToString();
                cbBState.Items.Add(st2);
            }
        }
    }

    public void restricted()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
            //permiso = long.Parse(user.getRestricted().ToString());
            RE_GenericBean permi = Utility.getPermisobyRuser(user, 15);
            permiso = permi.int64C2;
        }
        if ((permiso & 1) == 1) { Response.Redirect(Request.UrlReferrer.ToString()); }
        if ((permiso & 2) == 2) { Pinitial1.Visible = false; Pinitial2.Visible = false; }
    }

    public void permiso()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
            //permiso = long.Parse(user.getPermisoUser().ToString());
            RE_GenericBean permi = Utility.getPermisobyRuser(user, 15);
            permiso = permi.int64C1;
        }
        if ((permiso & 1) != 1) { lnk_update.Visible = false; }
        if ((permiso & 2) != 2) { Pinitial1.Enabled = false;  Pinitial2.Enabled =  false; }
    }
    protected void lnk_changepass_Click(object sender, System.EventArgs e)
    {
        lboldpass.CssClass = "style14";
        OldPass.Enabled = true;
        lbnewpass.CssClass = "style14";
        NewPass.Enabled = true;
        lblrepass.CssClass = "style14";
        repass.Enabled = true;
    }
    protected void lnk_changepic_Click(object sender, System.EventArgs e)
    {
        Label1.CssClass = "style12";
        Label1.Visible = true;
        CheckBox1.Enabled = true;
        CheckBox2.Enabled = true;
        CheckBox3.Enabled = true;
        Image2.Enabled = true;
        Image3.Enabled = true;
        Image4.Enabled = true;
        File1.Enabled = true;
    }
    protected void lnk_update_Click(object sender, System.EventArgs e)
    {
        if (validar() == 1)
        {
            cambiarLogo();
            string sxml = crearXml();
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            service.RegistraUsuario(sxml);
        }
        else
        {
            Response.Write("<script>alert('Unsuccessful process, please try again! ');</script>");
        }
    }

    public void cambiarLogo()
    {
        if (File1.HasFile)
        {
            try
            {
                string filepath = File1.FileName;
                File1.PostedFile.SaveAs(Server.MapPath("~//data/logos//U" + user.Text + "_" + filepath));
                imaurl = "~//data/logos//U" + user.Text + "_" + filepath;
                CheckBox1.Checked = false; CheckBox2.Checked = false; CheckBox3.Checked = false;
            }
            catch (Exception ex)
            {
                Span3.InnerHtml = "Error saving file <b>" + File1.PostedFile.FileName;
            }
        }
        else
        {
            if (CheckBox1.Checked) { imaurl = "~//data/logos//default.jpg"; }
            else if (CheckBox2.Checked) { imaurl = "~//data/logos//default1.png"; }
            else if (CheckBox3.Checked) { imaurl = "~//data/logos//logocasa.jpg"; }
            else if (userinfo != null) { imaurl = userinfo.getLogo(); }
        }
    }
}
